package at.jku.se.fastsell.app;

import java.util.List;
import at.jku.se.fastsell.app.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import at.jku.se.fastsell.data.Auction;
import at.jku.se.fastsell.rest.RestAccess;
import at.jku.se.fastsell.rest.ServiceException;

public class MainActivity extends Activity {
	private List<Auction> itemList;
	private ListView listView;
	public final static String SER_KEY = "com.example.fsapp.itemPass.ser";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.activity_main);
		final MainActivity parent = this;
		
		// TODO better use AsyncTask!
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy); 
		
		try {
			itemList = RestAccess.retrieveAllAuctions();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ItemAdapter adapter = new 	ItemAdapter(this,
                R.layout.list_item, itemList);
		
		listView = (ListView)findViewById(R.id.listView);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long rowId) {
				Intent intent = new Intent(parent, Detail.class);
				Auction item = itemList.get(Integer.parseInt(Long.toString(rowId)));
				Bundle mBundle = new Bundle();
				mBundle.putSerializable(SER_KEY, item);
				intent.putExtras(mBundle);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_logout){
			Session.currentUser = null;
			Intent intent = new Intent(this,LoginActivity.class);
			startActivity(intent);
		}else if(id == R.id.action_refresh){
			Intent intent = new Intent(this,LoginActivity.class);
			startActivity(intent);
		}else if (id == R.id.action_newAuction){
			Intent intent = new Intent(this,CreateAuctionActivity.class);
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.list_item, container,
					false);
			return rootView;
		}
	}

}
